package com.rf.richfitwheel.workflow.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rf.richfitwheel.admin.sys.vo.UserVO;
import com.rf.richfitwheel.common.contants.Constant;
import com.rf.richfitwheel.common.utils.PageUtils;
import com.rf.richfitwheel.common.utils.TreeUtils;
import com.rf.richfitwheel.common.utils.UuidUtil;
import com.rf.richfitwheel.workflow.conf.WorkflowContants;
import com.rf.richfitwheel.workflow.dao.WfProcessFolderMapper;
import com.rf.richfitwheel.workflow.model.WfProcessFolder;
import com.rf.richfitwheel.workflow.model.WfProcessFolderExample;
import com.rf.richfitwheel.workflow.service.WfProcessFolderService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Service("wfProcessFolderService")
public class WfProcessFolderServiceImpl implements WfProcessFolderService {
    @Autowired
    private WfProcessFolderMapper wfProcessFolderMapper;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        PageUtils pageUtils = PageUtils.getPageInfo(params);
        Page<Map<String, Object>> p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getLimit());
        WfProcessFolder wfProcessFolder = (WfProcessFolder) JSONObject.toBean(JSONObject.fromObject(params.get("dataForm")), WfProcessFolder.class);
        WfProcessFolderExample example = new WfProcessFolderExample();
        WfProcessFolderExample.Criteria criteria = example.createCriteria();
        List<WfProcessFolder> list = wfProcessFolderMapper.selectByExample(example);
        pageUtils = new PageUtils(p);
        return pageUtils;
    }

    @Override
    public void save(WfProcessFolder wfProcessFolder) {
        wfProcessFolder.setId(UuidUtil.get32UUID());
        wfProcessFolderMapper.insert(wfProcessFolder);
    }

    @Override
    public void updateById(WfProcessFolder wfProcessFolder) {
        wfProcessFolderMapper.updateByPrimaryKeySelective(wfProcessFolder);
    }

    @Override
    public void removeByIds(List<String> asList) {

    }

    @Override
    public WfProcessFolder getById(String id) {
        return wfProcessFolderMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<TreeUtils> getTreeForWfProcessFolder(String parentId) throws Exception {
        WfProcessFolderExample example = new WfProcessFolderExample();
        WfProcessFolderExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        example.setOrderByClause(" order_num ");
        List<WfProcessFolder> list = wfProcessFolderMapper.selectByExample(example);
        List<TreeUtils> treeList = TreeUtils.getTreeList(list, "id", "parentId", "folderName", "folderType", null, null);
        return treeList;
    }

    @Override
    public List<TreeUtils> treeListByIdForWfModelExtend(String parentId) throws Exception {
        WfProcessFolderExample example = new WfProcessFolderExample();
        WfProcessFolderExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        criteria.andDesignDisplayEqualTo("1");
        example.setOrderByClause(" order_num ");
        List<WfProcessFolder> list = wfProcessFolderMapper.selectByExample(example);
        List<TreeUtils> treeList = TreeUtils.getTreeList(list, "id", "parentId", "folderName", "folderType", null, null);
        return treeList;
    }

    @Override
    public WfProcessFolder selectByFolderCode(String folderCode) {
        WfProcessFolderExample example = new WfProcessFolderExample();
        WfProcessFolderExample.Criteria criteria = example.createCriteria();
        criteria.andFolderCodeEqualTo(folderCode);
        List<WfProcessFolder> wfProcessFolders = wfProcessFolderMapper.selectByExample(example);
        if (wfProcessFolders.size() > 0) {
            return wfProcessFolders.get(0);
        }
        return null;
    }

    @Override
    public void addWfProcessFolder(WfProcessFolder wfProcessFolder, UserVO userVO) {
        Date nowDate = new Date();
        /*if (!wfProcessFolder.getParentId().equals("3c64f3c8ae03445187e4a2d585695812")) {
            WfProcessFolder parentWpf = wfProcessFolderMapper.selectByPrimaryKey(wfProcessFolder.getParentId());
            wfProcessFolder.setModuleType(parentWpf.getModuleType());
        }*/
        wfProcessFolder.setId(UuidUtil.get32UUID());
        wfProcessFolder.setModuleType(wfProcessFolder.getModuleType());
        wfProcessFolder.setCreater(userVO.getId());
        wfProcessFolder.setCreateTime(nowDate);
        wfProcessFolder.setModTime(nowDate);
        wfProcessFolder.setModUser(userVO.getId());
        wfProcessFolderMapper.insert(wfProcessFolder);
    }

    @Override
    public void deleteById(String id, UserVO user) {
        wfProcessFolderMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void updateWfProcessFolder(WfProcessFolder wfProcessFolder, UserVO userVO) {
        Date nowDate = new Date();
        WfProcessFolder wpf = wfProcessFolderMapper.selectByPrimaryKey(wfProcessFolder.getId());
        /*if (!wfProcessFolder.getParentId().equals("3c64f3c8ae03445187e4a2d585695812")) {
            WfProcessFolder parentWpf = wfProcessFolderMapper.selectByPrimaryKey(wfProcessFolder.getParentId());
            wpf.setModuleType(parentWpf.getModuleType());
        }*/
        wpf.setFolderName(wfProcessFolder.getFolderName());
        wpf.setFolderCode(wfProcessFolder.getFolderCode());
        wpf.setFolderType(wfProcessFolder.getFolderType());
        wpf.setOrderNum(wfProcessFolder.getOrderNum());
        wpf.setIsCustomized(wfProcessFolder.getIsCustomized());
        wpf.setDesignDisplay(wfProcessFolder.getDesignDisplay());
        wpf.setFolderDesc(wfProcessFolder.getFolderDesc());
        wpf.setModuleType(wfProcessFolder.getModuleType());
        wpf.setModTime(nowDate);
        wpf.setModUser(userVO.getId());
        wfProcessFolderMapper.updateByPrimaryKeySelective(wpf);
    }

    @Override
    public List<WfProcessFolder> selectListByFolderType(String folderType) {
        WfProcessFolderExample example = new WfProcessFolderExample();
        WfProcessFolderExample.Criteria criteria = example.createCriteria();
        criteria.andFolderTypeEqualTo(folderType);
        List<WfProcessFolder> wfProcessFolders = wfProcessFolderMapper.selectByExample(example);
        return wfProcessFolders;
    }

    @Override
    public List<WfProcessFolder> selectListByFolderTypes(List<String> folderTypes) {
        WfProcessFolderExample example = new WfProcessFolderExample();
        WfProcessFolderExample.Criteria criteria = example.createCriteria();
        criteria.andFolderTypeIn(folderTypes);
        List<WfProcessFolder> wfProcessFolders = wfProcessFolderMapper.selectByExample(example);
        return wfProcessFolders;
    }

    @Override
    public List<WfProcessFolder> selectListByModuleType(String moduleType) {
        List<String> folderTypes = new ArrayList<>();
        folderTypes.add(WorkflowContants.FOLDER_TYPE_2);//流程大类
        folderTypes.add(WorkflowContants.FOLDER_TYPE_3);//通用流程
        WfProcessFolderExample example = new WfProcessFolderExample();
        WfProcessFolderExample.Criteria criteria = example.createCriteria();
        criteria.andFolderTypeIn(folderTypes);
        criteria.andModuleTypeEqualTo(moduleType);
        List<WfProcessFolder> wfProcessFolders = wfProcessFolderMapper.selectByExample(example);
        return wfProcessFolders;
    }
    
    @Override
    public List<TreeUtils> getTreeForWfProcessFolderForModuleType(String parentId, String moduleType) throws Exception{
        WfProcessFolderExample example = new WfProcessFolderExample();
        WfProcessFolderExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        List<String> modList = new ArrayList<>(Arrays.asList(moduleType.split(",")));
        modList.add(Constant.SystemModule.sys.name());
        criteria.andModuleTypeIn(modList);
        example.setOrderByClause(" order_num ");
        List<WfProcessFolder> list = wfProcessFolderMapper.selectByExample(example);
        List<TreeUtils> treeList = TreeUtils.getTreeList(list, "id", "parentId", "folderName", "folderType", null, null);
        return treeList;
    }

    /**
     * 获取按模块组织的流程定义
     *
     * @return 流程目录定义
     */
    @Override
    public Map<String, List<WfProcessFolder>> getWfProcessFolderForModuleType() {
        WfProcessFolderExample example = new WfProcessFolderExample();
        WfProcessFolderExample.Criteria criteria = example.createCriteria();
        criteria.andFolderTypeEqualTo(WorkflowContants.FOLDER_TYPE_2);
        Map<String, List<WfProcessFolder>> map = new HashMap<>();
        List<WfProcessFolder> list = wfProcessFolderMapper.selectByExample(example);
        list.forEach(m -> {
            if (map.containsKey(m.getModuleType())) {
                map.get(m.getModuleType()).add(m);
            }else{
                List<WfProcessFolder> temp = new ArrayList<>();
                temp.add(m);
                map.put(m.getModuleType(), temp);
            }
        });
        map.remove(null);
        return map;
    }
}